package org.dromara.pe.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.pe.domain.bo.AreaBo;
import org.dromara.pe.domain.vo.AreaVo;
import org.dromara.pe.domain.Area;
import org.dromara.pe.mapper.AreaMapper;
import org.dromara.pe.service.IAreaService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 体育场地设置Service业务层处理
 *
 * @author Lion Li
 * @date 2024-09-02
 */
@RequiredArgsConstructor
@Service
public class AreaServiceImpl implements IAreaService {

    private final AreaMapper baseMapper;

    /**
     * 查询体育场地设置
     */
    @Override
    public AreaVo queryById(Long areaId){
        return baseMapper.selectVoById(areaId);
    }

    /**
     * 查询体育场地设置列表
     */
    @Override
    public TableDataInfo<AreaVo> queryPageList(AreaBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Area> lqw = buildQueryWrapper(bo);
        Page<AreaVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询体育场地设置列表
     */
    @Override
    public List<AreaVo> queryList(AreaBo bo) {
        LambdaQueryWrapper<Area> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Area> buildQueryWrapper(AreaBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Area> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getAreaName()), Area::getAreaName, bo.getAreaName());
        lqw.eq(StringUtils.isNotBlank(bo.getNotes()), Area::getNotes, bo.getNotes());
        return lqw;
    }

    /**
     * 新增体育场地设置
     */
    @Override
    public Boolean insertByBo(AreaBo bo) {
        Area add = MapstructUtils.convert(bo, Area.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAreaId(add.getAreaId());
        }
        return flag;
    }

    /**
     * 修改体育场地设置
     */
    @Override
    public Boolean updateByBo(AreaBo bo) {
        Area update = MapstructUtils.convert(bo, Area.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Area entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除体育场地设置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
